<?php
/*------------------------------------------------------------------------------ 
    
    Create Date :2010-08-13
    Author    : Phan Van Dam
    Copyright(c) 2010 A.D.A Solution. All rights reserved
    
    ------------------------------------------------------------------------------
    Update History:
    Ver.    TRB#            Date          Author       Note
    3.0     thiếu comment  2011/03/10     HoangPhuc     Review code,update comment for source
------------------------------------------------------------------------------*/

if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
$objXajax->registerFunction('htfrmproduct_loadGrid');      
$objXajax->registerFunction('htfrmproduct_save');
$objXajax->registerFunction('htfrmproduct_edit');
$objXajax->registerFunction('htfrmproduct_delete');
$objXajax->registerFunction('htfrmproduct_fillMenuParent');

function htfrmproduct_save($params)
{
    global $objDbSelect,$user_id,$tbl_postfix;
    $objResponse =  new xajaxResponse();
      
    $name_vi = $params['txtnamemenuVi'];
    $name_en = $params['txtnamemenuEn'];
    $parent_id = $params['cbbMenuParent'];
    $price = $params['txtpricemenu'];
    $unit = $params['txtunitmenu'];
    $unitname = $params['txtnameunit'];
    $edit = $params['hdEdit'];
    $id = $params['hdId'];
    $sql = ""; 
    if($edit==1){
        $sql ="UPDATE ht_product$tbl_postfix SET name_vi = '$name_vi',name_en = '$name_en',parent_id = '$parent_id',
            price = '$price',unit = '$unit',unitname='$unitname',user_update = '$user_id',date_update = now() WHERE id =$id"; 
        $objDbSelect->Execute($sql);
        
    }else{
          $sql= " INSERT INTO ht_product$tbl_postfix(name_vi,name_en,active,unitname,price,parent_id,user_created,
          date_created,user_update,date_update) VALUES('$name_vi','$name_en',1,
          '$unitname','$price','$parent_id','$user_id',now(),'$user_id',now())"; 
          $objDbSelect->Execute($sql); 
     }    
    $objResponse->addScript('changeWaiting(1);');
    $objResponse->addScript('xajax_htfrmproduct_loadGrid();');
    return $objResponse->getXML();
}
function htfrmproduct_fillMenuParent()
{
    global $objDbSelect,$lang,$tbl_postfix;
    $objResponse =  new xajaxResponse();
    
    $sql = " SELECT id, name_$lang as name FROM ht_product$tbl_postfix WHERE 
       product_type = 0 ";   
    $arr = $objDbSelect->GetArray($sql);
    $count = count($arr);
    $temp ='';
    for($i=0;$i<$count;$i++){
        $id = $arr[$i]['id'];
        $name = $arr[$i]['name'];
        $temp.= "<option value='$id'> $name </option>"  ;
    }
    $temp =  '<select id="htfrmproduct_cbbMenuParent" class="cbbRequired"><option value="">--Chọn--</option>'.$temp.'</select>';
    $objResponse->addAssign('htfrmproduct_tdmenuparent', 'innerHTML',$temp);
    return $objResponse->getXML();
}
function htfrmproduct_loadGrid()
{ 
    global $lang,$path;
    $objResponse =  new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = htfrmproduct_getGrid();    
    $oTbs =  new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/ht_product_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('htfrmproduct_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTableGroup('htfrmproduct_tbl');");     
    $objResponse->addScript('changeWaiting(1);');                        
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}
function htfrmproduct_getGrid()
{
    global $objDbSelect,$lang,$tbl_postfix;
    $sqlSelect = " select pt1.id,pt1.active, pt1.name_vi,pt1.name_en,pt2.name_$lang as parent_name,pt1.price as price, 
                   pt1.unit as unit,pt1.icon as icon from ht_product$tbl_postfix as pt1 INNER JOIN  ht_product$tbl_postfix as pt2 ON 
                   pt1.parent_id = pt2.id WHERE  pt1.product_type =1  order by pt1.parent_id,pt1.name_$lang ";  
                  // echo $sqlSelect; die();
                                   
    return $objDbSelect->GetArray($sqlSelect);
}  

    function htfrmproduct_edit($id){
        global $objDbSelect,$tbl_postfix;
        $objResponse =  new xajaxResponse();
        $sql = " SELECT * FROM ht_product$tbl_postfix WHERE id = '$id' ";
        $arr = $objDbSelect->GetArray($sql);
        $name_vi        = $arr[0]['name_vi'];
        $name_en   = $arr[0]['name_en'];    
        $parent_id     = $arr[0]['parent_id'];
        $price         = $arr[0]['price'];       
        
        $objResponse->addScript("$('#htfrmproduct_txtnamemenuVi').val('$name_vi');");
        $objResponse->addScript("$('#htfrmproduct_txtnamemenuEn').val('$name_en');");
        $objResponse->addScript("$('#htfrmproduct_cbbMenuParent').val('$parent_id');");
        $objResponse->addScript("$('#htfrmproduct_txtpricemenu').val('$price');");
        $objResponse->addScript('changeWaiting(1);');
        return $objResponse->getXML();   
    }
    
?>
